package cn.deschen.eshop.common.config;

import cn.deschen.eshop.common.properties.Swagger2Properties;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description swagger2接口文档配置
 * @DateTime 2021/1/9 23:10
 * @Version V1.0.0
 */
public abstract class BaseSwagger2Config {

    @Bean
    public Docket createRestApi() {
        Swagger2Properties swagger2Properties = swagger2Properties();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(swagger2Properties))
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger2Properties.getApiBasePackage()))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    private ApiInfo apiInfo(Swagger2Properties swagger2Properties) {
        return new ApiInfoBuilder()
                .title(swagger2Properties.getTitle())
                .description(swagger2Properties.getDescription())
                .contact(new Contact(swagger2Properties.getContactName(), swagger2Properties.getContactUrl(), swagger2Properties.getContactEmail()))
                .version(swagger2Properties.getVersion())
                .build();
    }

    /**
     * 自定义Swagger配置
     */
    public abstract Swagger2Properties swagger2Properties();
}
